<?php

namespace App\Services\Index;

use App\Services\Service;
use App\Services\Traits\DatabaseMysql;
use App\Models\Game\Settlement\Info as SettlementInfo;
use App\Models\Game\Stats\Online;
use App\Models\Game\Pay\Order\Transfer;
use App\Models\Game\Pay\Order\Wallet;
use App\Core\Traits\Numeral;
use Helper\Timed;

class ChartService extends Service
{
    use DatabaseMysql, Numeral;

    /**
     * 统计图【在线人数】
     */
    public function online($request)
    {
        $query = Online::dbTable();
        // 筛选【平台】
        $this->makeWhereOrg($request, $query);
        // 天时间戳
        $timed = $request->input('datetime', Timed::getDayCurrentTimed());
        // 查询数据
        return $query->selectRaw('timed,SUM(online_num) AS online,SUM(gaming_num) AS gaming')
            ->where('timed', '>=', $timed)
            ->where('timed', '<', Timed::getDayNextTimed($timed))
            ->groupBy('timed')
            ->orderBy('timed')
            ->get();
    }

    /**
     * 统计图【平台收益】
     */
    public function amount($request)
    {
        $query = SettlementInfo::dbTable();
        // 筛选【平台】
        $this->makeWhereOrg($request, $query);
        // 筛选【时间】最近100天
        $this->makeWhereLastDays($request, $query, 'end_time', 100);
        // 统计数据
        return $query->selectRaw(implode(',', [
            "UNIX_TIMESTAMP(FROM_UNIXTIME(end_time, '%Y%m%d')) AS timed",
            "CAST(-SUM(usd->'$.gain_player') AS DECIMAL({$this->decimals()},{$this->precision()})) AS amount",
        ]))
            ->where('player_type', SettlementInfo::PLAYER_TYPE)
            ->groupBy('timed')
            ->get();
    }

    /**
     * 统计图【手工订单】
     */
    public function transfer($request)
    {
        // 转入
        $query = Transfer::dbTable();
        // 筛选【平台】
        $this->makeWhereOrg($request, $query);
        // 筛选【时间】最近100天
        $this->makeWhereLastDays($request, $query, 'created', 100);
        // 查询数据
        $in = $query->selectRaw(implode(',', [
            "UNIX_TIMESTAMP(FROM_UNIXTIME(created, '%Y%m%d')) AS timed",
            "CAST(SUM(usd->'$.amount') AS DECIMAL({$this->decimals()},{$this->precision()})) AS amount",
        ]))
            ->where('transfer_type', Transfer::TRANSFER_TYPE_IN)
            ->where('status', Transfer::STATUS_SUCCEED)
            ->groupBy('timed')
            ->get();

        // 转出
        $query = Transfer::dbTable();
        // 筛选【平台】
        $this->makeWhereOrg($request, $query);
        // 筛选【时间】最近100天
        $this->makeWhereLastDays($request, $query, 'created', 100);
        // 查询数据
        $out = $query->selectRaw(implode(',', [
            "UNIX_TIMESTAMP(FROM_UNIXTIME(created, '%Y%m%d')) AS timed",
            "CAST(SUM(usd->'$.amount') AS DECIMAL({$this->decimals()},{$this->precision()})) AS amount",
        ]))
            ->where('transfer_type', Transfer::TRANSFER_TYPE_OUT)
            ->where('status', Transfer::STATUS_SUCCEED)
            ->groupBy('timed')
            ->get();

        return [
            'in' => $in,
            'out' => $out
        ];
    }

    /**
     * 统计图【钱包订单】
     */
    public function wallet($request)
    {
        // 转入
        $query = Wallet::dbTable();
        // 筛选【平台】
        $this->makeWhereOrg($request, $query);
        // 筛选【时间】最近100天
        $this->makeWhereLastDays($request, $query, 'created', 100);
        // 查询数据
        $data = $query->selectRaw(implode(',', [
            "UNIX_TIMESTAMP(FROM_UNIXTIME(created, '%Y%m%d')) AS timed",
            "CAST(SUM(usd->'$.consume_sum') AS DECIMAL({$this->decimals()},{$this->precision()})) AS consume_sum",
            "CAST(SUM(usd->'$.take_back_sum') AS DECIMAL({$this->decimals()},{$this->precision()})) AS take_back_sum",
        ]))
            ->where('status', Wallet::STATUS_SUCCEED)
            ->groupBy('timed')
            ->get();

        return $data;
    }
}
